package com.kk.goGame.service.impl;

import com.kk.goGame.entity.Email;
import com.kk.goGame.entity.ScoreDetails;
import com.kk.goGame.entity.User;
import com.kk.goGame.mapper.UserMapper;
import com.kk.goGame.mapper.impl.UserMapperImpl;
import com.kk.goGame.service.IScoreDetailsService;
import com.kk.goGame.service.IUserService;
import com.kk.goGame.utils.CodeUtils;
import com.kk.goGame.utils.EmailUtils;
import com.kk.goGame.utils.Md5Utils;
import com.qf.framwork.utils.ConnectionUtils;

import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author kk
 * @since 2023-02-14
 */
public class UserServiceImpl implements IUserService {
    private UserMapper userMapper = new UserMapperImpl();
    private IScoreDetailsService scoreDetailsService = new ScoreDetailsServiceImpl();

    @Override
    public User login(String username, String password) {
        User user = userMapper.checkColumn("username", username);
        if (user != null) {
            String passwordMd5 = Md5Utils.str2MD5(password);
            if (user.getPassword().equals(passwordMd5)) {
                user.setPassword(null);
                return user;
            }
        }
        return null;
    }

    @Override
    public Integer register(User user) {
        User flag = userMapper.checkColumn("username", user.getUsername());
        if (flag != null) {
            return -1;
        }
        flag = userMapper.checkColumn("email", user.getEmail());
        if (flag != null) {
            return -2;
        }
        flag = userMapper.checkColumn("phone", user.getPhone());
        if (flag != null) {
            return -3;
        }
        user.setPassword(Md5Utils.str2MD5(user.getPassword()));
        Integer i = userMapper.insertUser(user);
        System.out.println("新增的数据id是" + i);
        return i;
    }

    @Override
    public User checkColumn(String column, Object value) {
        return userMapper.checkColumn(column, value);
    }

    @Override
    public User sedCode(String username, String findtype) {
        User user = userMapper.checkColumn("username", username);
        if (user == null) return null;
        Integer code = CodeUtils.createCode();
        CodeUtils.setCode(username, code);
        if (findtype.equals("1")) {
            System.out.println("开始执行发送邮件验证码");
            Email email = new Email()
                    .setTo(user.getEmail())
                    .setContent(code+ ",非本人操作请忽略")
                    .setSubject("修改密码");
            EmailUtils.sendEmail(email);
        } else if (findtype.equals("2")) {
            System.out.println("发送手机短信验证");
        }
        return user;
    }

    @Override
    public int updatePassword(String newPassword, String username) {
        String s = Md5Utils.str2MD5(newPassword);
        return userMapper.updatePassword(s, username);
    }

    @Override
    public Integer updateScore(Integer uid, Integer score, String busid, Integer source) {
        try {
            ConnectionUtils.begin();

            int result = userMapper.updateScore(uid, score);
            if (result == 1) {
                //生成积分流水
                ScoreDetails scoreDetails = new ScoreDetails()
                        .setUid(uid)
                        .setScore(Math.abs(score))
                        .setAction(score > 0 ? 0 : 1)
                        .setSource(source)
                        .setBusinessId(busid)
                        .setCreateTime(LocalDateTime.now())
                        .setUpdateTime(LocalDateTime.now())
                        .setStatus(1)
                        .setDelFlag(0);
                scoreDetailsService.insertScoreDetails(scoreDetails);
            }

            ConnectionUtils.commit();
        } catch (SQLException e) {
            ConnectionUtils.rollback(e);
        }
        return 1;
    }

    @Override
    public Map<String,Integer> tongjiSex() {
        Map<String,Integer> map = userMapper.tongjiSex();
        return map;
    }

    @Override
    public List<User> tongjiScore() {
        return userMapper.tongjiScore();
    }
}
